package com.chaintor.demo.math;

import org.apache.commons.math3.analysis.integration.SimpsonIntegrator;
import org.apache.commons.math3.analysis.integration.UnivariateIntegrator;

import java.util.Vector;

public class BspLine {
    //关键点数
    private int _K;

    //曲线节点，一共有K+6个点
    private double[] _D;

    //基础函数
    private BspBaseFunc baseFunc;


    /**
     * 构造函数
     * @param keys        关键点
     //* @param prices      样本债券全价数组
     //* @param cashFlows   样本债券现金流数组
     */
    public BspLine(double[] keys){
        _K = keys.length;
        //扩充曲线节点
        _D = new double[_K+6];
        _D[0] = _D[1]= _D[2] = keys[0];
        for (int i=0;i<keys.length;i++){
            _D[i+3] =  keys[i];
        }
        _D[_K+3] =  _D[_K+4] = _D[_K+5] = keys[_K-1];

        baseFunc = new BspBaseFunc(keys);

    }

    public double[][] getH(){
        double[][] h = new double[_K+2][_K+2];
        for (int i=0;i<_K+2;i++){
            for (int j=0;j<_K+2;j++){
                BspIntegralFunc bif = new BspIntegralFunc(i+1,j+1,baseFunc,16,-2,1.5);
                UnivariateIntegrator integrator = new SimpsonIntegrator();
                h[i][j] = integrator.integrate(10000, bif, 0,_D[_K+3] );
            }
        }
        return h;
    }

}
